﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using COSXML;
using COSXML.Auth;
using COSXML.Model.Bucket;
using COSXML.Model.Tag;


namespace COSSnippet
{
    
    #region
    
    public class ListObjectsModel
    {

        private CosXml cosXml;
        private string nextMarker;

        public ListObjectsModel()
        {
            CosXmlConfig config = new CosXmlConfig.Builder()
              .SetRegion("ap-shanghai") // 设置默认的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
              .Build();

            string secretId = ("AKIDNf2PoDQfjgaHtJZI2mLhoi1eEeuAF9sP"); //用户的 SecretId，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
            string secretKey = ("Yv8A1iFbnphNJ4eQOEzkGex6CC5qOhkW");  // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
            long durationSecond = 600;          //每次请求签名有效时长，单位为秒
            QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,
              secretKey, durationSecond);

            this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
        }

        /// 获取对象列表
        public void GetBucket()
        {
            //.cssg-snippet-body-start:[get-bucket]
            try
            {
                // 存储桶名称，此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
                string bucket = "document-manage-1317383760";
                GetBucketRequest request = new GetBucketRequest(bucket);
                //执行请求
                GetBucketResult result = cosXml.GetBucket(request);
                //bucket的相关信息
                ListBucket info = result.listBucket;
                var contents1 = new ListBucket.Contents();
                //Console.WriteLine(info.maxKeys+"\n");
                if (info.isTruncated)
                {
                    // 数据被截断，记录下数据下标
                    this.nextMarker = info.nextMarker;
                }
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                //请求失败
                Console.WriteLine("CosClientException: " + clientEx);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                //请求失败
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }

            //.cssg-snippet-body-end
        }

        /// 获取第二页对象列表
        public void GetBucketNextPage()
        {
            //.cssg-snippet-body-start:[get-bucket-next-page]
            try
            {
                // 存储桶名称，此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
                string bucket = "document-manage-1317383760";
                GetBucketRequest request = new GetBucketRequest(bucket);
                //上一次拉取数据的下标
                request.SetMarker(this.nextMarker);
                //执行请求
                GetBucketResult result = cosXml.GetBucket(request);
                //bucket的相关信息
                ListBucket info = result.listBucket;
                //Console.WriteLine(info+"\n");
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                //请求失败
                Console.WriteLine("CosClientException: " + clientEx);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                //请求失败
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }
            //.cssg-snippet-body-end
        }

        /// 获取对象列表与子目录
        public string[] GetBucketWithDelimiter()
        {
            int i = 0, j = 0;
            string [] table_office;
            //.cssg-snippet-body-start:[get-bucket-with-delimiter]
            
                // 存储桶名称，此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
                string bucket = "document-manage-1317383760";
                GetBucketRequest request = new GetBucketRequest(bucket);
                //获取 a/ 下的对象以及子目录
                request.SetPrefix("医院文件/");
                request.SetDelimiter("/");

                //执行请求
                GetBucketResult result = cosXml.GetBucket(request);
                //bucket的相关信息
                ListBucket info = result.listBucket;
                // 对象列表
                List<ListBucket.Contents> objects = info.contentsList;
                
                // 子目录列表
                List<ListBucket.CommonPrefixes> subDirs = info.commonPrefixesList;

                foreach (ListBucket.CommonPrefixes obj in subDirs)
                {
                    Console.WriteLine("文件夹："+obj.prefix);
                    
                    i++;
                }
                table_office = new string[i];
                foreach (ListBucket.CommonPrefixes obj in subDirs)
                {
                    Console.WriteLine("文件夹：" + obj.prefix);
                    table_office[j] = obj.prefix;
                    j++;
                }
            
            //catch (COSXML.CosException.CosClientException clientEx)
            //{
            //    //请求失败
            //    Console.WriteLine("CosClientException: " + clientEx);
            //}
            //catch (COSXML.CosException.CosServerException serverEx)
            //{
            //    //请求失败
            //    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            //}
            //.cssg-snippet-body-end
            return table_office;
        }

        public string[] GetBucketWithDelimiter_office(string office)
        {
            int i = 0, j = 0;
            string[] table_office;
            //.cssg-snippet-body-start:[get-bucket-with-delimiter]

            // 存储桶名称，此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
            string bucket = "document-manage-1317383760";
            GetBucketRequest request = new GetBucketRequest(bucket);
            //获取 a/ 下的对象以及子目录
            //string office1 = "医院文件/";
            request.SetPrefix(office);
            request.SetDelimiter("/");

            //执行请求
            GetBucketResult result = cosXml.GetBucket(request);
            //bucket的相关信息
            ListBucket info = result.listBucket;
            // 对象列表
            List<ListBucket.Contents> objects = info.contentsList;

            // 子目录列表
            List<ListBucket.CommonPrefixes> subDirs = info.commonPrefixesList;

            foreach (ListBucket.CommonPrefixes obj in subDirs)
            {
                Console.WriteLine("文件夹：" + obj.prefix);

                i++;
            }
            table_office = new string[i];
            foreach (ListBucket.CommonPrefixes obj in subDirs)
            {
                Console.WriteLine("文件夹：" + obj.prefix);
                table_office[j] = obj.prefix;
                j++;
            }

            //catch (COSXML.CosException.CosClientException clientEx)
            //{
            //    //请求失败
            //    Console.WriteLine("CosClientException: " + clientEx);
            //}
            //catch (COSXML.CosException.CosServerException serverEx)
            //{
            //    //请求失败
            //    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            //}
            //.cssg-snippet-body-end
            return table_office;
        }
        public string[] GetBucketWithDelimiter_document(string office)
        {
            int i = 0, j = 0;
            string[] table_document;
            //.cssg-snippet-body-start:[get-bucket-with-delimiter]

            // 存储桶名称，此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
            string bucket = "document-manage-1317383760";
            GetBucketRequest request = new GetBucketRequest(bucket);
            //获取 a/ 下的对象以及子目录
            //string office1 = "医院文件/";
            request.SetPrefix(office);
            request.SetDelimiter("/");

            //执行请求
            GetBucketResult result = cosXml.GetBucket(request);
            //bucket的相关信息
            ListBucket info = result.listBucket;
            // 对象列表
            List<ListBucket.Contents> objects = info.contentsList;

            // 子目录列表
            List<ListBucket.CommonPrefixes> subDirs = info.commonPrefixesList;


            foreach (ListBucket.Contents obj in objects)
            {
                i++;
            }
            //MessageBox.Show(i.ToString());
            table_document = new string[i];
            string[] table = new string[i - 1];
            foreach (ListBucket.Contents obj in objects)
            {
                table_document[j] = obj.key;
                j++;
            }
            //foreach (string a in table_document)
            //{
            //    if (z < j-1)
            //        table_document[z] = table_document[z + 1];
            //    else
            //        break;
            //    z++;
            //}
            for (int zzz = 0; zzz < i - 1; zzz++)
            {
                table[zzz] = table_document[zzz + 1];
            }

            //foreach (string a in table)
            //{
            //    MessageBox.Show(a);
            //}
            return table;
        }
        public string[] GetBucketWithDelimiter2()
        {
            
            int i = 0, j = 0;
            string[] table_document;
            //.cssg-snippet-body-start:[get-bucket-with-delimiter]
            
            // 存储桶名称，此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
            string bucket = "document-manage-1317383760";
            GetBucketRequest request = new GetBucketRequest(bucket);
            //获取 a/ 下的对象以及子目录
            request.SetPrefix("医院文件/");
            request.SetDelimiter("/");

            //执行请求
            GetBucketResult result = cosXml.GetBucket(request);
            //bucket的相关信息
            ListBucket info = result.listBucket;
            // 对象列表
            List<ListBucket.Contents> objects = info.contentsList;

            // 子目录列表
            List<ListBucket.CommonPrefixes> subDirs = info.commonPrefixesList;


            foreach (ListBucket.Contents obj in objects)
            {
                i++;
            }
            //MessageBox.Show(i.ToString());
            table_document = new string[i];
            string[] table = new string[i - 1];
            foreach (ListBucket.Contents obj in objects)
            {
                table_document[j] = obj.key;
                j++;
            }
            //foreach (string a in table_document)
            //{
            //    if (z < j-1)
            //        table_document[z] = table_document[z + 1];
            //    else
            //        break;
            //    z++;
            //}
            for (int zzz = 0; zzz <i-1 ; zzz++)
            {
                table[zzz] = table_document[zzz + 1];
            }

            //foreach (string a in table)
            //{
            //    MessageBox.Show(a);
            //}
            return table;
        }
        // .cssg-methods-pragma

        static void Main(string[] args)
        {
            ListObjectsModel m = new ListObjectsModel();

            /// 获取对象列表
            m.GetBucket();
            /// 获取第二页对象列表
            m.GetBucketNextPage();
            /// 获取对象列表与子目录
            m.GetBucketWithDelimiter();
            // .cssg-methods-pragma
        }
    }
    
    #endregion
}